/*
 * Copyright 2006 - 2007 - Chris Maki
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.theserverside.blog.model;

import java.io.Serializable;

import javax.persistence.AssociationOverride;
import javax.persistence.AssociationOverrides;
import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

import org.apache.commons.lang.builder.ToStringBuilder;

@Entity
@Table(name="USER")
@AttributeOverrides( { @AttributeOverride(name = "id", column = @Column(name = "USER_ID")),
    @AttributeOverride(name="version", column=@Column(name="USER_VERSION")),
    @AttributeOverride(name="dateCreated", column=@Column(name="USER_DATE_CREATED")),
    @AttributeOverride(name="dateUpdated", column=@Column(name="USER_DATE_UPDATED"))
    })
@AssociationOverrides( {
    @AssociationOverride(name="createdByUser", joinColumns=@JoinColumn(name="USER_CREATED_BY_USER_ID")),
    @AssociationOverride(name="updatedByUser", joinColumns=@JoinColumn(name="USER_UPDATED_BY_USER_ID"))
})

@NamedQueries( {
    @NamedQuery(name="user.findByName", query="select u from User u where u.username like ?1")
} )
public class User extends ModelBase implements Serializable {

    private static final long serialVersionUID = -7966330655353574056L;

   @Column(name="USER_USERNAME", nullable=false, unique=true)
    private String username;
    
    @Column(length=32, name="USER_PASSWORD", nullable=false)
    private String password;

    
    @Embedded
    @AttributeOverrides( { 
        @AttributeOverride(name="firstName", column=@Column(name="USER_FIRST_NAME")),
        @AttributeOverride(name="lastName", column=@Column(name="USER_LAST_NAME")),
        @AttributeOverride(name="bio", column=@Column(name="USER_BIO")),
        @AttributeOverride(name="dateOfBirth", column=@Column(name="USER_DATE_OF_BIRTH")),
        @AttributeOverride(name="emailAddress", column=@Column(name="USER_EMAIL")),
        @AttributeOverride(name="passwordHintQuestion", column=@Column(name="USER_PASSWORD_QUESTION")),
        @AttributeOverride(name="passwordHintAnswer", column=@Column(name="USER_PASSWORD_ANSWER")),
        @AttributeOverride(name="signature", column=@Column(name="USER_SIGNATURE")),
        @AttributeOverride(name="lastLogin", column=@Column(name="USER_LASTLOGIN")),
        @AttributeOverride(name="avatar", column=@Column(name="USER_AVATAR"))
        } )
    private UserDetail userDetail = new UserDetail();
    


    public UserDetail getUserDetail() {
        return userDetail;
    }

    public void setUserDetail(UserDetail detail) {
        this.userDetail = detail;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String userName) {
        this.username = userName;
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this)
            .append("user id: ", getId())
            .append("username : ", getUsername())
            .toString();
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((username == null) ? 0 : username.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null) return false;
        if (getClass() != obj.getClass()) return false;
        final User other = (User) obj;
        if (username == null) {
            if (other.getUsername() != null) return false;
        }
        else if (!username.equals(other.getUsername())) return false;
        return true;
    }
    
    
}

